package com.carey.blog.action.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;


import com.carey.blog.framework.latke.action.AbstractAction;
import com.carey.blog.framework.latke.action.ActionException;
import com.carey.blog.framework.latke.service.LangPropsService;
import com.carey.blog.framework.latke.service.ServiceException;
import com.carey.blog.framework.latke.util.Locales;
import com.carey.blog.listener.CareyBlogServletListener;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * Carey Blog initialization action. init.ftl.
 * 
 */
public final class InitAction extends AbstractAction {

	/**
	 * Default serial version uid.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger.getLogger(InitAction.class
			.getName());
	/**
	 * FreeMarker configuration.
	 */
	private Configuration configuration;
	/**
	 * Language service.
	 */
	private LangPropsService langPropsService = LangPropsService.getInstance();

	@Override
	protected Map<?, ?> doFreeMarkerAction(
			final freemarker.template.Template template,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		final Map<String, Object> ret = new HashMap<String, Object>();
		
		try {
			final Map<String, String> langs = langPropsService.getAll(Locales
					.getLocale(request));
			ret.putAll(langs);
		} catch (final ServiceException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
		}

		try {
			if (CareyBlogServletListener.isInited()) {
				response.sendRedirect("/");
				return ret;
			}
		} catch (final Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
		}

		return ret;
	}

	@Override
	protected JSONObject doAjaxAction(final JSONObject data,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void init() throws ServletException {
		configuration = new Configuration();
		configuration.setDefaultEncoding("UTF-8");
		
		try {
			final String webRootPath = CareyBlogServletListener.getWebRoot();

			configuration.setDirectoryForTemplateLoading(new File(webRootPath));
		} catch (final IOException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
		}
	}

	@Override
	protected Template beforeDoFreeMarkerAction(
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		final String pageName = getPageName(request.getRequestURI());

		try {
			return configuration.getTemplate(pageName);
		} catch (final IOException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw new ActionException(e);
		}
	}
}
